<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996 15899929162
// +----------------------------------------------------------------------
namespace app\common\dao\energy;

use think\db\BaseQuery;
use app\common\dao\BaseDao;
use app\common\model\energy\EnergySharedRoomModel;

class EnergySharedRoomDao extends BaseDao
{

    /**
     * @return EnergySharedRoomModel
     */
    protected function getModel(): string
    {
        return EnergySharedRoomModel::class;
    }

    /**
     * @param array $where
     * @return BaseQuery
     */
    public function search(array $where, int $propertyId = null)
    {
        return $this->getModel()::getDB()
            ->when($propertyId !== null, function ($query) use ($propertyId) {
                $query->where('property_id', $propertyId);
            })
            ->when(isset($where['plan_id']) && $where['plan_id'] !== '', function ($query) use ($where) {
                $query->where('plan_id', intval($where['plan_id']));
            })
            ->when(isset($where['is_settle']) && $where['is_settle'] !== '', function ($query) use ($where) {
                $query->where('is_settle', intval($where['is_settle']));
            })
            ->when(isset($where['room_id']) && $where['room_id'] !== '', function ($query) use ($where) {
                $query->where('room_id', intval($where['room_id']));
            })
            ->when(isset($where['energy_id']) && $where['energy_id'] !== '', function ($query) use ($where) {
                $query->where('energy_id', intval($where['energy_id']));
            })
            ->when(isset($where['keywords']) && $where['keywords'] !== '', function ($query) use ($where) {
                $query->whereLike('plan_name|remark', '%' . trim($where['keywords']) . '%');
            });

    }

    /**
     * 根据公摊方案 删除
     * @param int $planId
     * @return bool
     *
     * @author Amos
     * 2024-05-19 11:42
     */
    public function removePlanId(int $planId)
    {
        return $this->getSearch([
            'plan_id' => $planId,
        ])->delete();
    }
}
